package cn.piesat.controller;

import cn.piesat.service.intf.DeptInfoService;
import cn.piesat.util.ResponseWrapperUtil;
import cn.piesat.vo.deptinfo.SaveDeptInfoVO;
import com.alibaba.fastjson.JSONArray;
import cn.piesat.dto.PiesatDeptInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * 部门信息接口
 *
 * @author xhl
 */
@RestController
@RequestMapping("userDept")
public class DeptInfoController {

    @Autowired
    private DeptInfoService deptInfoService;

    /**
     * 添加部门信息
     * @param request
     * @param saveDeptInfoVO
     * @return
     */
    @PostMapping("/addDept")
    public String addDeptInfo(HttpServletRequest request, @Valid @RequestBody SaveDeptInfoVO saveDeptInfoVO) {
        Map<String,String> resultMap = null;
        try {
            resultMap = deptInfoService.add(saveDeptInfoVO,request);
        } catch (Exception e) {
            return ResponseWrapperUtil.executeCustomTips("false","添加部门接口异常:" + e.getMessage());
        }
        return ResponseWrapperUtil.executeCustomTips(resultMap.get("success"),resultMap.get("message"));
    }

    /**
     * 修改部门信息
     * @param request
     * @param saveDeptInfoVO
     * @return
     */
    @PostMapping("modifyDept")
    public String updateDeptInfo(HttpServletRequest request, @Valid @RequestBody SaveDeptInfoVO saveDeptInfoVO) {
        Map<String,String> resultMap = null;
        try {
            resultMap = deptInfoService.update(saveDeptInfoVO,request);
        } catch (Exception e) {
            return ResponseWrapperUtil.executeCustomTips("false","修改部门接口异常:" + e.getMessage());
        }
        return ResponseWrapperUtil.executeCustomTips(resultMap.get("success"),resultMap.get("message"));
    }

    /**
     * 删除部门信息及其子级部门，并将绑定了这些部门的用户的部门设置为null
     * @param id 部门ID
     * @return
     */
    @DeleteMapping("/delDept/{id}")
    public String del(@PathVariable("id")String id) {
        Map<String,String> resultMap = null;
        try {
            resultMap = deptInfoService.del(id);
        } catch (Exception e) {
            return ResponseWrapperUtil.executeCustomTips("false","删除部门信息接口异常" + e.getMessage());
        }
        return ResponseWrapperUtil.executeCustomTips(resultMap.get("success"),resultMap.get("message"));
    }

    /**
     * 获取部门详细信息
     * @param id 部门ID
     * @return
     */
    @GetMapping("/get/{id}")
    public String getById(@PathVariable("id") String id) {
        PiesatDeptInfoDTO piesatDeptInfoDTO = deptInfoService.findById(id);
        if (piesatDeptInfoDTO == null) {
            return ResponseWrapperUtil.executeCustomTips("false","该信息不存在");
        }
        return ResponseWrapperUtil.executeResultEntity(piesatDeptInfoDTO);
    }

    /**
     * 查询树形结构数据，不需要分页，按排序号排序
     * @return
     */
    @GetMapping("getAll")
    public String getAll() {
        JSONArray resultArray = deptInfoService.getAll();
        return ResponseWrapperUtil.executeResultList(resultArray);
    }
}
